import { getList, addItem, deleteItem, updateItem } from '@/services/unitTest';

const Model = {
    namespace: 'unitTest',
    state: {
        list: [],
    },
    effects: {
        *requestList({ payload }, { call, put }) {
            const response = yield call(getList, payload);
            if (response.error) {
                yield put({
                    type: 'update',
                    payload: {
                        list: [],
                    },
                });
            } else {
                yield put({
                    type: 'update',
                    payload: {
                        list: response,
                    },
                });
            }
        },
        *requestAddItem({ payload }, { call, put, select }) {
            const value = yield select(state => state.hooks.count);
            console.log(value);
            const response = yield call(addItem, payload);
            if (response.error) {
                yield put({
                    type: 'update',
                    payload: {
                        list: [],
                    },
                });
            } else {
                yield put({
                    type: 'update',
                    payload: {
                        list: response,
                    },
                });
            }
        },
        *requestDeleteItem({ payload }, { call, put }) {
            const response = yield call(deleteItem, payload);
            yield put({
                type: 'update',
                payload: {
                    list: response,
                },
            });
        },
        *requestUpdateItem({ payload }, { call, put }) {
            const response = yield call(updateItem, payload);
            yield put({
                type: 'update',
                payload: {
                    list: response,
                },
            });
        },
    },
    reducers: {
        update(state, { payload }) {
            return {
                ...state,
                ...payload,
            };
        },
    },
};

export default Model;
